package com.lty.practice.rabbitmq.waiter.integration;

import com.lty.practice.rabbitmq.waiter.model.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OrderListener {
    @Autowired
    @Qualifier(Customer.FINISHED_ORDERS)
    private MessageChannel finishedOrdersMessageChannel;
    @Value("${order.chef-prefix}${random.uuid}")
    private String chef;

    @StreamListener(Customer.NEW_ORDERS)
    public void processNewOrder(Order order) throws InterruptedException {
        log.info("Receive a new Order {}.",order);
        Thread.sleep(5000);
        order.setChef(this.chef);
        log.info("Order {} is READY.", order);
        finishedOrdersMessageChannel.send(MessageBuilder.withPayload(order).build());
    }
}
